/*
 * NewJInternalFrame.java
 *
 * Created on 2008년 1월 6일 (일), 오전 12:39
 */

package PuriON;

import PuriON_Chat.*;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
 *
 * @author  hyukmini
 */
public class PURION_Consume extends javax.swing.JInternalFrame {
    
    PURION_Main parent;
    DefaultListModel model;
    private String ret;

    private Thread t;
    
    /** Creates new form NewJInternalFrame */
    public PURION_Consume(PURION_Main p) {
        super("스트리밍 목록 보기");
        parent = p;
        initComponents();
        this.setVisible(true);
        setLocation(30, 30);
        
        //08.05.30 이진우 PURION_Connect 수정중 추가 부분
        //목록을 얻어와 뿌린다
        //전달 메시지는 LIST
        //정상 리턴값은 방 목록
        //방이 없으면 :ANS 리턴
        
        //*개선점 : LIST로 방이름 뿐만아니라 IP도 얻어오자!
        
        //혁민이형이 이용했던 DefaultListModel을 listbox와 연결하여 insert한다.
        model = new DefaultListModel();
        model.removeAllElements();        
        roomList.setModel(model);
        
        PURION_Connect sock = new PURION_Connect(parent);
        String message = "LIST";
        ret = sock.sendMessage(message);
        
        if(ret.equals(":ANS")){
            model.add(0,"개설된 방이 없습니다.");
        }else{
            String[] splitString = ret.split("\\|");
            for(int i = 0; i<splitString.length; i++){
                model.add(i,splitString[i]);
            }
        }
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        listPane = new javax.swing.JScrollPane();
        roomList = new javax.swing.JList();

        setClosable(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PuriON/reources/Music File.png"))); // NOI18N
        jLabel1.setText("  현재 방송중인 방 목록 입니다.");

        jButton1.setText("선택한 방에 들어갑니다.");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        roomList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                roomListValueChanged(evt);
            }
        });
        listPane.setViewportView(roomList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listPane, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(listPane, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    


    public void sendQuery() {

        PURION_Connect listquery;
        
        String query = "SELECT * FROM ShareList";
        listquery = new PURION_Connect(parent);
        listquery.sendMessage(query);
                
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        parent.addComopnent(new PURION_Consumer(parent, ret, (String)model.getElementAt(roomList.getSelectedIndex())));
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

private void roomListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_roomListValueChanged
// TODO add your handling code here:
    //08.05.30 이진우 PURION_Connect 수정중 추가 부분
    //선택한 방제목에 해당하는 IP를 적는다.
    //전달 메시지는 VIEW|방제목
    //정상 리턴값은 IP
    //방이 없으면 :ANS 리턴
    PURION_Connect sock = new PURION_Connect(parent);
    if (roomList.getSelectedIndex() > -1) {
        String roomname = (String)model.getElementAt(roomList.getSelectedIndex());
        String message = "VIEW|"+roomname;

        ret = sock.sendMessage(message);

        if(!ret.equals(":ANS")){
            jTextField1.setText(ret);
        }
    }
    
    
}//GEN-LAST:event_roomListValueChanged

private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
// TODO add your handling code here:
    //chater.messageSend("사용자가 나갔습니다.");
}//GEN-LAST:event_formInternalFrameClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JScrollPane listPane;
    private javax.swing.JList roomList;
    // End of variables declaration//GEN-END:variables
    
}
